var wagWolframAlpha = {
	
	wolframResult: "",
	
	init: function (){
		//alert ("Wolfram loaded");
		this.wolframLoaded();
	},
	
  wolframLoaded: function (event, doc, wolframIframe){
		//Will need to use another method for notifiying about page load.
		//Can't access cross domain in Chrome
		//http://ejohn.org/blog/cross-window-messaging/#postcomment
		//http://groups.google.com/group/chromium-extensions/browse_thread/thread/861f5b56916b18aa
		//http://code.google.com/chrome/extensions/xhr.html
		//alert (document.jsonArray);
		//document.jsonArray = {"popups":{}, "imgs":{}};
		context = parent ? parent : document;
    try {
		$ = context.$;
	} 
	catch (e) {
	}//alert (e) } //context = window;
    //alert ("$: " + $)
		var mainDoc = doc;
		var wolframDoc = document;
		var wolframResult = this.checkWolframResult (wolframDoc);
    this.hideElement("header", wolframDoc);
    this.hideElement("calculate", wolframDoc);
    this.hideElement("sidebar", wolframDoc);
    this.hideElement("footer", wolframDoc);
		this.hideElement("timeout", wolframDoc);
		wagWolframAlpha.wolframResult = wolframResult;
  },

	checkWolframResult: function (wolframDoc){
		var resultType = "";
		if (wolframDoc.getElementById("future-img"))resultType = "kaihatsu";
		else if (wolframDoc.getElementById("splat"))resultType = "wakaranai";
		else resultType = "kekkaAru";
		return resultType;
	},

  hideElement: function (elementId, doc){
    if (!doc) doc = document;
    var currentElement = doc.getElementById(elementId);
    if (currentElement) currentElement.style.display="none"
  },
	
	receiveMessage: function (event)
	{
	  //if (event.origin !== "http://example.org:8080")
	  //  return;
		var googleWindow = event.source;
		while (wagWolframAlpha.wolframResult == ""){
			//Do nothing. When result is available, program continues.
		}
		googleWindow.postMessage(wagWolframAlpha.wolframResult, "*");
	},

}

wagWolframAlpha.init();

window.addEventListener("message", wagWolframAlpha.receiveMessage, false);

